interface response<T> {
  code: number
  data: T
  msg: string
}

Page({
  data: {
    isRefreshing: false,
    jokeList: [] as string[],
    num: 10
  },
  onLoad() {
    this.getData()
      .then((res) => {
        this.setData({
          jokeList: res
        })
      })
  },
  getData() {
    return new Promise<string[]>((resolve, reject) => {
      wx.showLoading({
        title: '加载中'
      })
      wx.request<response<string[]>>({
        url: 'https://api-vue-base.itheima.net/api/joke/list',
        method: 'GET',
        data: {
          num: 10 //this.data.num
        },
        success: (res) => {
          //wx.hideLoading()
          console.log(res)
          // this.setData({
          //   jokeList: res.data.data
          // })
          resolve(res.data.data)
        },
        fail: () => {
          reject('失败')
        },
        complete: () => {
          wx.hideLoading()
        },
      })
    })

  },
  async onRefresh() {
    this.setData({
      isRefreshing: true
    })
    const res = await this.getData()
    this.setData({
      jokeList: res,
      isRefreshing: false
    })
  },
  async onReachBottom() {
    const res = await this.getData()
    this.setData({
      jokeList: [...this.data.jokeList, ...res]
    })
    // wx.showToast({ title: '触底加载' })
  },
})